﻿using Abp.Application.Services.Dto;
using Logistics.EntityFrameworkCore;
using Logistics.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Application.Dtos;


namespace Logistics.Recipients
{
    public class ClaimAppService : ApplicationService, IClaimAppService
    {
        private readonly IRepository<Claim, int> _claimRepository;
        private readonly IRepository<MaterialType, int> _materialTypes;
        private readonly LogisticsDbContext _logisticsDbContext;

        public ClaimAppService(IRepository<Claim, int> claimRepository, IRepository<MaterialType, int> materialTypes, LogisticsDbContext logisticsDbContext)
        {
            _logisticsDbContext = logisticsDbContext;
            _claimRepository = claimRepository;
            _materialTypes = materialTypes;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="Procurement"></param>
        /// <returns></returns>
        public async Task<ClaimDto> CreateAsync(ClaimDto Claim)
        {
            var claim = new Claim
            {
                Number = Claim.Number,
                TypeName = Claim.TypeName,
                ProcurementTypeId = Claim.ProcurementTypeId,
                PurposeDescription = Claim.PurposeDescription,
                Receiver=Claim.Receiver,
                Remarks=Claim.Remarks,
                State=Claim.State,
                Title=Claim.Title,
                UserId=Claim.UserId,
                ClaimTime=DateTime.Now,
                CreationTime=DateTime.Now,
            };

            await _claimRepository.InsertAsync(claim);
            return ObjectMapper.Map<Claim, ClaimDto>(claim);
        }

        [RemoteService(false)]
        public async Task DeleteAsync(int id)
        {
            await _claimRepository.DeleteAsync(id);
        }

        public async Task<ClaimDto> GetAsync(int id)
        {
            var claim = await _claimRepository.GetAsync(id);
            return ObjectMapper.Map<Claim, ClaimDto>(claim);
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<Volo.Abp.Application.Dtos.PagedResultDto<ClaimDto>> GetClaimListAsync(GetClaimListDto dto)
        {
            //var all = _ProcurementRepository.ToList();
            var all = _logisticsDbContext.Claim.ToList();
            var type = _logisticsDbContext.MaterialType.ToList();
            var procType = from a in all
                           join c in type on a.ProcurementTypeId equals c.Id
                           select new Claim
                           {
                               Number=a.Number,
                               TypeName=c.Name,
                               ProcurementTypeId=a.ProcurementTypeId,
                               PurposeDescription=a.PurposeDescription,
                               Receiver=a.Receiver,
                               Remarks=a.Remarks,
                               State=a.State,
                               Title=a.Title,
                               UserId=a.UserId,
                               ClaimTime=DateTime.Now,
                               CreationTime=DateTime.Now,
                               Id=a.Id,
                           };
            int totoCount = procType.Count();
            var data = new Volo.Abp.Application.Dtos.PagedResultDto<ClaimDto>();
            data.TotalCount = totoCount;
            procType = procType.Skip((dto.page - 1) * dto.limit).Take(dto.limit).ToList();
            data.Items = ObjectMapper.Map<List<Claim>, List<ClaimDto>>(procType.ToList());
            return data;
        }

        public int UpdateAsync(ClaimDto Claim)
        {
            var claim = new Claim
            {
                Number = Claim.Number,
                TypeName=Claim.TypeName,
                ProcurementTypeId = Claim.ProcurementTypeId,
                PurposeDescription= Claim.PurposeDescription,
                Receiver= Claim.Receiver,
                Remarks= Claim.Remarks,
                State= Claim.State,
                Title=Claim.Title,
                UserId = Claim.UserId,
                ClaimTime=DateTime.Now,
                CreationTime= DateTime.Now,
                Id=Claim.Id,
            };
            _logisticsDbContext.Entry(claim).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            var i = _logisticsDbContext.SaveChanges();
            return i;
        }
    }
}
